package com.greateamd.timetableasy.server.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.greateamd.timetableasy.client.domain.AuthType;
import com.greateamd.timetableasy.client.services.user.UserService;
import com.greateamd.timetableasy.domain.Event;
import com.greateamd.timetableasy.domain.User;
import com.greateamd.timetableasy.domain.exceptions.DBServerSideException;
import com.greateamd.timetableasy.domain.exceptions.IllegalEntityStateException;
import com.greateamd.timetableasy.server.ServerUtil;
import com.greateamd.timetableasy.server.dao.UserDAO;

@Service("userService")
public class UserServiceImpl implements UserService {

	private UserDAO userDAO;
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	@Override
	public boolean save(User user) throws IllegalEntityStateException,DBServerSideException {
		try {
			userDAO.save(user);
		} catch (IllegalEntityStateException e) {
			throw e;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
		return true;
	}

	@Override
	public User find(String username) throws DBServerSideException  {
		try {
			return userDAO.getById(username);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public boolean update(User user) throws DBServerSideException  {
		try {
			userDAO.update(user);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}
	
	@Override
	public List<User> getAll() throws DBServerSideException  {
		try {
			return userDAO.getAll();
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public boolean remove(User user) throws DBServerSideException {
		try {
			userDAO.delete(user);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<User> getAllCampusManager() throws DBServerSideException,IllegalEntityStateException {
		try {
			return userDAO.getAllCampusManager();
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<User> getUserWithoutPromotion() throws DBServerSideException, IllegalEntityStateException {
		try {
			return userDAO.getUserWithoutPromotion();
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<User> getAllTeachers() throws DBServerSideException,IllegalEntityStateException {
		try {
			return userDAO.getAllTeachers();
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<Event> getPlanning(User user, Date from, Date to) throws DBServerSideException, IllegalEntityStateException {
		try {
			return userDAO.getPlanning(user,from,to);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<Event> getTeacherPlanning(User user, Date fromDate, Date toDate) {
		try {
			List<Event> events = new ArrayList<Event>();
			events.addAll(userDAO.getTeacherPlanning(user,fromDate,toDate));
			ServerUtil.sortStartDateList(events);
			return events;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}
	
	@Override
	public boolean addUserAuth(User user, AuthType auth) throws DBServerSideException, IllegalEntityStateException {
		try {
			userDAO.addUserAuth(user,auth);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public boolean removeUserAuth(User user, AuthType auth) throws DBServerSideException, IllegalEntityStateException {
		try {
			userDAO.removeUserAuth(user,auth);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public boolean getTeacherDisponibility(Integer excluseThisEventId,String teacherUsername, Date from, int duration) throws DBServerSideException, IllegalEntityStateException {
		try {
			return userDAO.getTeacherDisponibility(excluseThisEventId,teacherUsername, from, duration);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public boolean addEvent(Event event) throws DBServerSideException,IllegalEntityStateException {
		try {
			return userDAO.addEvent(event);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}
}
